﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace MainApp
{
    /// <summary>
    /// Interaction logic for LogonScreen.xaml
    /// </summary>
    public partial class LogonScreen : Window
    {
        public static bool bIsLogonSuccess;
        private const int GWL_STYLE = -16;
        private const int WS_SYSMENU = 0X00080000;

        [DllImport( "user32.dll" )]
        private extern static int SetWindowLong( IntPtr hwnd, int index, int value );
        [DllImport( "user32.dll" )]
        private extern static int GetWindowLong( IntPtr hwnd, int index );

        public LogonScreen()
        {
            InitializeComponent();
            bIsLogonSuccess = false;
            this.txtUserName.Focus();
            this.ResizeMode = System.Windows.ResizeMode.NoResize;
            this.WindowStyle = System.Windows.WindowStyle.ThreeDBorderWindow;

            SourceInitialized += MainWindow_SourceInitialized;
            InitializeComponent();
        }

        void MainWindow_SourceInitialized( object sender, EventArgs e )
        {
            WindowInteropHelper wih = new WindowInteropHelper( this );
            int style = GetWindowLong( wih.Handle, GWL_STYLE );
            SetWindowLong( wih.Handle, GWL_STYLE, style & ~WS_SYSMENU );
        }

        private void Button_KeyUp( object sender, KeyEventArgs e )
        {
            if ( Key.Enter == e.Key )
            {
                Button_Click( sender, e );
            }
        }

        private void Button_Click( object sender, RoutedEventArgs e )
        {
            if ( this.txtUserName.Text == "abc" && this.pwPassword.Password == "123" )
            {
                bIsLogonSuccess = true;
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                bIsLogonSuccess = false;
                MessageBox.Show( "Invalid username or password.\nTry again" );
                this.txtUserName.Text = string.Empty;
                this.pwPassword.Password = string.Empty;
            }
        }
    }
}
